package com.wbw.csnd_hd.initcache;

import com.wbw.csnd_hd.Utils.RedisUtils;
import com.wbw.csnd_hd.common.ConstantCommon;
import com.wbw.csnd_hd.mapper.AreaMapper;
import com.wbw.csnd_hd.pojo.Area;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * redis缓存预热
 */
@Component
public class InitProduct {

    @Autowired
    RedisUtils redisUtils;
    @Autowired
    AreaMapper areaMapper;

    //在服务器运行启动完成前先执行一次
    @PostConstruct
    public void initArea(){
        List<Area> areas = areaMapper.find();
        //缓存库区相关的所有数据
        List<String> collect = areas.parallelStream().map(Area::getNo).collect(Collectors.toList());
        redisUtils.ssetCollection(ConstantCommon.AREA.AREA_ID_ALL,collect);

        //由于存储详细信息，数据较多，所有使用多线程异步来进行存储
        CompletableFuture.runAsync(()->{
            areas.stream().forEach(area -> {
                String no = area.getNo();
                String s = ConstantCommon.AREA.AREA_ID_PERFIX + no;
                redisUtils.hmset(s,area);
            });
        });

    }


}
